<#assign object = doc.objects>
package ${object.package}.service;

import java.io.PrintWriter;

public class ModelViewService {

	public void getModelNames(PrintWriter out) {
		out.print("[");
		<#list object["/objects/object/name"] as modelname>
		out.print("{");
		out.print("\"objectName\":");
		out.print("\"");
		out.print("${modelname}");
		out.print("\"");
		out.print("}"); 
		<#if modelname_has_next>
		out.print(",");
		</#if>
		</#list>
		out.print("]");
	}
	
	public void getModelFields(String modelName, PrintWriter out){
		out.print("[");
		<#list object["/objects/object"] as model>
		if(modelName.equalsIgnoreCase("${model.name}")){
			<#assign n = 0 />
				<#list model.attributes?children as att>
					<#if att?node_type == "element">
						<#assign n = n+1 />
			<#if n gt 1>
			out.print(",");
			</#if>
			//add field_name
			out.print("{");
			out.print("\"field_name\":");
			out.print("\"");
						<#if att?node_name != "set" && att?node_name != "object">
			out.print("${att.@name[0]!att.@class?uncap_first}");
						<#elseif att?node_name == "set">
			out.print("${att.@name[0]!att.@class?uncap_first}s");
						<#elseif att?node_name == "object">
			out.print("${att.@name[0]!att.@class?uncap_first}");
						</#if>
			out.print("\"");
			out.print(",");
				
			//add field_type 	
			out.print("\"field_type\":");
			out.print("\"");
			out.print("${att?node_name}");
			out.print("\"");
			out.print(",");
			
			//add not_null attribute	
			out.print("\"not_null\":");
			out.print("\"");
						<#if att.@notnull[0]??>
							<#if att.@notnull[0] == "true">
			out.print("true");
							<#else>
			out.print("false");
							</#if>
						<#else>
			out.print("false");
						</#if>
			out.print("\"");
			out.print(",");
			
			//add regexp attribute
						<#if att.@regexp[0]??>
			out.print("\"regexp\":");
			out.print("\"");
			out.print("${att.@regexp}");
			out.print("\"");
			out.print(",");
						</#if>
			
			//add maxlength attribute				
						<#if att.@maxlength[0]??>
			out.print("\"maxlength\":");
			out.print("\"");
			out.print("${att.@maxlength}");
			out.print("\"");
			out.print(",");
						</#if>
			
			//add finder attribute	
			out.print("\"finder\":");
			out.print("\"");
						<#if att.@finder[0]??>
							<#if att.@finder[0] == "true">
			out.print("true");
							<#else>
			out.print("false");
							</#if>
						<#else>
			out.print("false");
						</#if>
			out.print("\"");
			out.print(",");
			
			//add lazy attribute
			out.print("\"lazy\":");
			out.print("\"");
						<#if att.@lazy[0]??>
							<#if att.@lazy[0] == "true">
			out.print("true");
							<#else>
			out.print("false");
							</#if>
						<#else>
			out.print("false");
						</#if>
			out.print("\"");
			
			out.print("}");
				</#if>
				</#list>
			}
		</#list>	
		out.print("]");
	}
	
	public void getFindFields(String modelName, PrintWriter out){
		out.print("[");
		<#list object["/objects/object"] as model>
		if(modelName.equalsIgnoreCase("${model.name}")){
			<#assign n = 0 />
			<#list model.attributes?children as att>
				<#if att.@finder[0]?? && att.@finder[0] == "true">
					<#assign n = n+1 />
					<#if n gt 1>
					out.print(",");
					</#if>
					out.print("{");
					out.print("\"finder_field_name\":");
					out.print("\"${att.@name}\"");
					out.print(",");
					out.print("\"finder_field_method\":");
					out.print("\"findBy${att.@name?cap_first}\"");
					out.print("}");
				</#if>
			</#list>
		}
		</#list>
		out.print("]");
	}
	
}
